<!DOCTYPE html>
<html>
<head>
    <title>入院办理</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">

        // 出院退院ajax请求
        function backCourtResetClearAjax(method, hosrState, hosrId){
            layer.load(0)
            $.ajax({
                type: "POST",
                // 1. url请求地址前加后端API地址origin
                url: origin + "/register/" + hosrId,
                data: {_method:"PUT",hosrState:hosrState,jwtToken:jwtToken},
                dataType: "json",
                // 2. 允许携带cookie跨域请求
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    layer.closeAll();
                    if(data.code == 200){
                        if(method == 0){
                            // 出院
                            layer.msg("出院成功")
                            $("#td_" + hosrId).prev().text("已出院")
                        }else{
                            // 退院
                            layer.msg("退院成功")
                            $("#td_" + hosrId).prev().text("已退院")
                        }
                        $("#td_" + hosrId).empty();
                        $("#td_" + hosrId).append("<a href='look.html?hosrId="+hosrId+"&pageNum="+globalPageNum+"&pageSize="+globalPageSize+"'>详情>>></a>")
                    }else{
                        layer.msg(data.msg, function(){})
                    }
                },
                complete : function(xhr, status) {
                    myComplete(xhr, status);
                }
            })
        }
        // 退院处理
        function backCourt(hosrId){
            let hosrState = 3; // 退号
            backCourtResetClearAjax(1, hosrState, hosrId);
        }
        // 出院处理
        function outHospital(hosrId){
            let hosrState = 2; // 出院
            backCourtResetClearAjax(0, hosrState, hosrId);
        }
        // 监听清空
        function resetClear(){
            $("button[type='reset']").on("click", function(){
                $("input").val("");
                queryList(1);
            })
        }

        // 查询列表
        queryList = function(pageNum){
            // 病历号
            let hosrId = $("input[name='hosrId']").val().trim()
            // 医生名字
            let doctorName = $("input[name='doctorName']").val().trim()
            // 医生所属科室
            let dKeshi = $("input[name='dKeshi']").val().trim()
            // 挂号开始时间
            let createTimeStartBlur = $("input[name='createTimeStartBlur']").val().trim()
            // 挂号结束时间
            let createTimeEndBlur = $("input[name='createTimeEndBlur']").val().trim()
            layer.load(0)
            $.ajax({
                type: "GET",
                url: origin + "/be-hospital/list",
                data: {pageNum:pageNum,pageSize:globalPageSize,hosrId:hosrId,doctorName:doctorName,
                    dKeshi:dKeshi,createTimeStartBlur:createTimeStartBlur,createTimeEndBlur:createTimeEndBlur,jwtToken:jwtToken},
                // 允许携带cookie跨域请求
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    layer.closeAll();
                    if(data.code === 200){
                        // 填充分页
                        fillPageData(data.obj);
                        // 填充内容
                        fillContentData(data.obj.list);
                        // 更新全局页数信息
                        globalPageSize = data.obj.pageSize;
                        globalPageNum = data.obj.pageNum;
                    }else{
                        layer.msg(data.msg,function(){})
                    }
                },
                complete : function(xhr, status) {
                    myComplete(xhr, status);
                }
            });
        }
        // 填充内容
        function fillContentData(cont){
            // 先清空数据
            $("table.content").find("tbody").empty();
            // 遍历数据
            $.each(cont, function(){
                // 病人信息
                let beHospitalMsg = this.beHospital;
                // 结算状态
                let behClosePriceState =  beHospitalMsg.behClosePrice;
                // 渲染显示状态
                let behStateText = "";
                // 填充信息
                let $con = $(" <tr >\n" +
                    "            <td style=\"vertical-align:middle;\"><input type=\"checkbox\" name=\"check\" value=\"" + this.hosrId + "\"></td>\n" +
                    "            <td style=\"vertical-align:middle;\">" + this.hosrId + "</td>\n" +
                    "            <td style=\"vertical-align:middle;\">" + this.hosrName + "</td>\n" +
                    "            <td style=\"vertical-align:middle;\">"+ beHospitalMsg.behPatBed +"</td>\n" +
                    "            <td style=\"vertical-align:middle;\">"+ this.hosrPhone +"</td>\n" +
                    "            <td style=\"vertical-align:middle;\">"+ beHospitalMsg.behAntecedent +"元</td>\n" +
                    "            <td style=\"vertical-align:middle;\">" + this.doctorName + "</td>\n" +
                    "            <td style=\"vertical-align:middle;\">" + this.hosrCreateTimeStr + "</td>\n" +
                    "            <td style=\"vertical-align:middle;\">" + this.keshi + "</td>\n" +
                    "</tr>");
                // 条件显示 -> 状态列 & 操作列
                if(behClosePriceState == 0){
                    // 出院住院退院状态
                    let behState = beHospitalMsg.behState;
                    if(this.hosrState == 1){
                        // **已住院** -> 详情 更改 缴纳押金
                        behStateText = "已住院";
                        $con.append("<td style=\"vertical-align:middle;\">"+ behStateText +"</td>")
                        $con.append("<td id='td_"+ this.hosrId +"'style='vertical-align:middle;'><a href='look.html?hosrId="+this.hosrId+"&pageNum="+globalPageNum+"&pageSize="+globalPageSize+"'>详情>>></a>&nbsp;&nbsp;&nbsp;<a href='edit.html?behId="+beHospitalMsg.behId+"&hosrId="+ this.hosrId +"&pageNum="+globalPageNum+"&pageSize="+globalPageSize+"'>更改</a>&nbsp;&nbsp;&nbsp;<a href='add_many.html?behId="+beHospitalMsg.behId+"&hosrId="+ this.hosrId +"&pageNum="+globalPageNum+"&pageSize="+globalPageSize+"'>缴纳押金</a></td>\n");
                    }else if(this.hosrState == 2){
                        // **已出院** -> 详情
                        behStateText = "已出院";
                        $con.append("<td style=\"vertical-align:middle;\">"+ behStateText +"</td>")
                        $con.append("<td id='td_"+ this.hosrId +"'style='vertical-align:middle;'><a href='look.html?hosrId="+this.hosrId+"&pageNum="+globalPageNum+"&pageSize="+globalPageSize+"'>详情>>></a></td>\n");
                    }else{
                        // **已退院** -> 详情
                        behStateText = "已退院";
                        $con.append("<td style=\"vertical-align:middle;\">"+ behStateText +"</td>")
                        $con.append("<td id='td_"+ this.hosrId +"'style='vertical-align:middle;'><a href='look.html?hosrId="+this.hosrId+"&pageNum="+globalPageNum+"&pageSize="+globalPageSize+"'>详情>>></a></td>\n");
                    }
                }else{
                    if(this.hosrState == 1){
                        // **已结算** -> 详情 退院 出院
                        behStateText = "已结算";
                        $con.append("<td style=\"vertical-align:middle;\">"+ behStateText +"</td>")
                        $con.append("<td id='td_"+ this.hosrId +"'style='vertical-align:middle;'><a href='look.html?hosrId="+this.hosrId+"&pageNum="+globalPageNum+"&pageSize="+globalPageSize+"'>详情>>></a>&nbsp;&nbsp;&nbsp;<a href='javascript:if (confirm(\"确认操作?\")) {backCourt(" + this.hosrId + ");}'>退院</a>&nbsp;&nbsp;&nbsp;<a href='javascript:if (confirm(\"确认操作?\")) {outHospital(" + this.hosrId + ")}'>出院</a></td>\n");
                    }else if(this.hosrState == 2){
                        // **已出院** -> 详情
                        behStateText = "已出院";
                        $con.append("<td style=\"vertical-align:middle;\">"+ behStateText +"</td>")
                        $con.append("<td id='td_"+ this.hosrId +"'style='vertical-align:middle;'><a href='look.html?hosrId="+this.hosrId+"&pageNum="+globalPageNum+"&pageSize="+globalPageSize+"'>详情>>></a></td>\n");
                    }else{
                        // **已退院** -> 详情
                        behStateText = "已退院";
                        $con.append("<td style=\"vertical-align:middle;\">"+ behStateText +"</td>")
                        $con.append("<td id='td_"+ this.hosrId +"'style='vertical-align:middle;'><a href='look.html?hosrId="+this.hosrId+"&pageNum="+globalPageNum+"&pageSize="+globalPageSize+"'>详情>>></a></td>\n");
                    }
                }
                $("table.content").find("tbody").append($con);
            })

        }
	
    	function checkall(){
			var alls=document.getElementsByName("check");
			var ch=document.getElementById("checkall");
			if(ch.checked){
				for(var i=0;i<alls.length;i++){
					alls[i].checked=true;	
				}	
			}else {
                for (var i = 0; i < alls.length; i++) {
                    alls[i].checked = false;
                }
            }
		}

        // mode: 0:出院 1:退院
        function delAll(mode) {
            var alls = document.getElementsByName("check");
            var ids = new Array();
            for (var i = 0; i < alls.length; i++) {
                if (alls[i].checked) {
                    let cha = $("#td_" + alls[i].value).find("a").eq(1);
                    if (cha.length == 0 || cha.text() != '退院') {
                        // xxx病号还未结算
                        layer.msg(alls[i].value + "病号还未结算", function () {});
                        setTimeout(function(){
                            alls[i].checked = false;
                        },300)
                        return false;
                    }
                    ids.push(alls[i].value);
                }
            }
            if (ids.length > 0) {
                if (confirm("确认操作?")) {
                    for (let i = 0; i < ids.length; i++) {
                        if (mode == 0) { // 出院
                            // 出院处理
                            outHospital(ids[i]);
                        } else {  // 退院
                            // 退院处理
                            backCourt(ids[i]);
                        }
                    }
                    setTimeout(function(){
                        $("input[type='checkbox']").prop("checked",false);
                    },1000)
                }
            }else{
                alert("请选中要操作的项");
            }
        }

        // 导出Excel
        function exportExcel(){
            var alls=document.getElementsByName("check");
            var ids = new Array();
            for(var i=0;i<alls.length;i++){
                if(alls[i].checked){
                    ids.push(alls[i].value);
                }
            }
            if(ids.length > 0){
                // 导出
                let url = origin + "/be-hospital/export";
                reqExport(url, "住院病人信息", ids, "hosrId");
            }else{
                alert("请选中要操作的项");
            }
        }

        $(function () {
            // 查询第一页
            queryList(globalPageNum);
            // 监听清空
            resetClear();
            $('#newNav').click(function(){
                window.location.href="add.html?pageSize="+globalPageSize+"&pageNum="+globalPageNum;
            });
        });
    </script>
</head>
<body>

<div class="definewidth m20">
<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <td width="10%" class="tableleft">病历号：</td>
        <td><input placeholder="请输入病历号" type="text" name="hosrId" value=""/></td>

        <td width="10%" class="tableleft">主治医生：</td>
        <td><input placeholder="请输入主治医生姓名" type="text" name="doctorName" value=""/></td>

        <td width="10%" class="tableleft">科室：</td>
        <td><input placeholder="请输入科室名称" type="text" name="dKeshi" value=""/></td>
    </tr>
    <tr>

        <td width="10%" class="tableleft">挂号时间：</td>

        <td colspan="5">
            <input type="text" placeholder="2099-01-01 00:00:00" name="createTimeStartBlur" value=""/>&nbsp;至&nbsp;<input placeholder="2099-01-01 00:00:00" type="text" name="createTimeEndBlur" value=""/>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <button type="button" class="query btn btn-primary" onclick="queryList(1)">查询</button>
            <button type="reset" class="btn btn-primary">清空</button>
        </td>
    </tr>
</table>
</div>
   
<table class="content table table-bordered table-hover definewidth m10" >
   <thead>
    <tr>
    	<th><input type="checkbox" id="checkall" onChange="checkall();"></th>
        <th>病历号</th>
        <th>姓名</th>
        <th>床位号</th>
        <th>联系电话</th>
        <th>押金</th>
        <th>主治医生</th>
        <th>入院时间</th>
        <th>科室</th>
        <th>状态</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody></tbody>
  </table>
  
  <table class="table table-bordered table-hover definewidth m10" >
  	<tr><th colspan="5">
        <div id="pageInfo-box" class="inline pull-right page"></div>
		 <div><button type="button" class="btn btn-success" id="newNav">添加住院信息</button>&nbsp;&nbsp;&nbsp;
		 <button type="button" class="btn btn-success" onClick="delAll(0);">出院</button>&nbsp;&nbsp;&nbsp;
		 <button type="button" class="btn btn-success" onClick="delAll(1);">退院</button>&nbsp;&nbsp;&nbsp;
		 <button type="button" class="btn btn-success" onclick="exportExcel()">导出Excel</button>
		 </div>
		 
    </th></tr>
  </table>
  
</body>
</html>
